import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import parsing.KochhomeParser;
import parsing.LearningEntry;

/**
 * 
 */

/**
 * @author rick
 *
 */
public class Program {
	public static String newline = System.getProperty("line.separator");
	private static PrintStream out; 
	private static PrintStream err; 
	private static List<LearningEntry> entries;
	/**
	 * @param args
	 * @throws Exception 
	 */
	private static int nbAsk = 0;
	private static int nbGood = 0; 
	private static Map<String,Integer> bads = new HashMap<String,Integer>();
	public static void main(String[] args) throws Exception {
		out = new PrintStream(System.out, true, "UTF8"); 
		err = new PrintStream(System.err, true, "UTF8"); 
		entries = KochhomeParser.getLesson(1);
		entries.addAll(KochhomeParser.getLesson(2));
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		err.println("Write '!end' to end the game!");
		
		for(LearningEntry e : entries){
			bads.put(e.getFrenchTranslation(), 0);
		}
		while(true)
		{	
			Collections.shuffle(entries);
			for(LearningEntry e : entries)
			{
				err.flush();
				out.flush();
				out.println("----------------------------------");
				out.println("French: " + e.getFrenchTranslation());
				out.print("German: ");
				String answer = in.readLine();
				if(answer.startsWith("!")){
					if(!doCommand(answer.substring(1)))
						return;
				}
				else
				{
					nbAsk++;
					if(e.tryAnswer(answer)){
						out.println("Bravo !!");
						nbGood++;
					}
					else{
						err.println("Wrong ! Righ answer: " + e.getGermanTranslation());
						bads.put(e.getFrenchTranslation(), 1+bads.get(e.getFrenchTranslation()));
					}
				}
			}
		}
	}
	private static boolean doCommand(String s) {
		String c = s.toLowerCase();
		if( c.equals("end")){
			if(nbAsk > 0){
				out.println();
				err.flush();
				out.flush();
				out.println("----------------------------------");
				out.println("------------- SCORES -------------");
				out.println(((float)nbGood*100/(float)nbAsk)+"% (" +nbGood+"/"+nbAsk+")");
				out.println("------------- ERRORS -------------");
				
				for(LearningEntry e : entries){
					if(bads.get(e.getFrenchTranslation()) > 0)
						out.println(e.getFrenchTranslation() + "  =>  " + e.getGermanTranslation() + "   (" + bads.get(e.getFrenchTranslation()) + " fois)");
				}
				out.println("----------------------------------");
			}
			return false;
		}
		return true;
	}

}
